﻿using System.Collections.Generic; 
using System.Linq; 
using DevExpress.Utils; 
using YDServices;
using YDModel;
using YDCommon;
using System;
using System.Windows.Forms;
using System.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using static YDServices.Enum.OrderEnum;  
using YDWin.Sending;
using YDWin.Handle;

namespace YDWin.Deying
{
    public partial class DeyingForm : DevExpress.XtraEditors.XtraForm
    {

        List<T_Order_Details> opList = null;
        T_Order_Details item = null; 
        DeyingService svr = new DeyingService();
        FactoryService fs = new FactoryService();
        public DeyingForm()
        {
            InitializeComponent();

            Global.GirdViewSet(this.gvDeying);
            

            //翻页事件
            pageControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);

            //设置日期控件格式
            this.dtpStartDate.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.dtpStartDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpStartDate.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.dtpStartDate.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpStartDate.Properties.Mask.EditMask = "yyyy年MM月dd日";

            this.dtpEndDate.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.dtpEndDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpEndDate.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.dtpEndDate.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpEndDate.Properties.Mask.EditMask = "yyyy年MM月dd日";

  
        }
        /// <summary>
        /// 自动行号 自增长
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvDeying_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadDataGrid();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadDataGrid()
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在查询......");

            item = null;
            ConditionItem[] conds = GetConditions();
            if (conds != null)
            {
                opList = svr.getList(conds);
                sdf.Close();
                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "载入失败\n" + svr.Errors);
                    svr.Errors.Clear();
                }
                else
                {
                    List<T_Order_Details> list = opList.Skip((pageControl1.PageIndex - 1) * pageControl1.PageSize).Take(pageControl1.PageSize).ToList();
                    pageControl1.DrawControl(opList.Count());
                    if (list.Count() == 0)
                    {
                        bsData.DataSource = null;
                    }
                    else
                    {
                        bsData.DataSource = list;
                        this.gvDeying.MoveFirst();//移动到第一条记录
                        //UIChecked.BestFit();
                    }
                }
            }
           

        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private ConditionItem[] GetConditions()
        {
            List<ConditionItem> conds = new List<ConditionItem>();

            conds.Add(new ConditionItem
            {
                PropertyName = "Factory_Name",
                FindType = ConditionType.ctLike,
                Values = new object[] { cboFactoryName.Text }
            });

            conds.Add(new ConditionItem
            {
                PropertyName = "OperatintTime",
                FindType = ConditionType.ctGreaterThanOrEqual,
                Values = new object[] { dtpStartDate.EditValue }
            });

            conds.Add(new ConditionItem
            {
                PropertyName = "OperatintTime",
                FindType = ConditionType.ctLessThanOrEqual,
                Values = new object[] { dtpEndDate.EditValue }
            });

            return conds.ToArray();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            LoadDataGrid();
        }


        /// <summary>
        /// 点击事件与gvDeying.OptionsBehavior.EditorShowMode 设置对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvDeying_Click(object sender, EventArgs e)
        {
            item = bsData.Current as T_Order_Details;
        }
        /// <summary>
        /// 鼠标事件 与gvDeying.OptionsBehavior.EditorShowMode 设置对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void gvDeying_MouseDown_1(object sender, MouseEventArgs e)
        //{
        //    item = bsData.Current as T_Order;
        //}


        /// <summary>
        /// 加载完事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeyingForm_Load(object sender, EventArgs e)
        {
            //厂家名称下拉框 染厂
            var fDs = fs.getComboxList((int)OrderTyp.染厂,"");
            cboFactoryName.Properties.ValueMember = "ID";   //实际要用的字段
            cboFactoryName.Properties.DisplayMember = "FactoryName";    //要显示的字段
            cboFactoryName.Properties.DataSource = fDs;
            cboFactoryName.Properties.SearchMode = DevExpress.XtraEditors.Controls.SearchMode.AutoComplete;//输入之后自动显示并选中下拉内容


            dtpStartDate.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);//月初1号
            dtpEndDate.EditValue = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day);//当日+1

        }

   
         

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }

            if (item.UseQuantity > 0 || item.UseWeight > 0)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "该记录已被使用,不能修改!");
                return;
            }

            AddDeyingForm form = new AddDeyingForm();
            form.Text = "编辑染厂订单信息";
            form.state = "Edit";
            form.editTu = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }

            if (item.UseQuantity > 0 || item.UseWeight > 0)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "该记录已被使用,不能删除!");
                return;
            }

            DialogResult dr = Utils.ShowPrompt(MessageType.mtInformation, "即将删除该记录,删除之后将不可恢复!", null, MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            svr.DelItem(item);
            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "删除失败\n" + svr.Errors);
                svr.Errors.Clear();
            }
            else
            {
                Utils.ShowPrompt(MessageType.mtInformation, "删除成功\n");
                LoadDataGrid();
            }
        }

     

        /// <summary>
        /// 拉毛
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTranslate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }
            if (item.SurplusQuantity <= 0 || string.IsNullOrEmpty(item.SurplusQuantity.ToString()))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "库存不足!");
                return;
            }

            if (item.SurplusWeight <= 0 || string.IsNullOrEmpty(item.SurplusWeight.ToString()))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "库存不足!");
                return;
            }

            AddHandleForm form = new AddHandleForm();
            form.Text = "新增后加工订单信息";
            form.state = "Add";
            form.editLi = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        /// <summary>
        /// 发货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSending_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }
            if (item.SurplusQuantity <= 0 || string.IsNullOrEmpty(item.SurplusQuantity.ToString()))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "库存不足!");
                return;
            }

            if (item.SurplusWeight <= 0 || string.IsNullOrEmpty(item.SurplusWeight.ToString()))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "库存不足!");
                return;
            }

            AddSendingForm form = new AddSendingForm();
            form.Text = "新增发货订单信息";
            form.state = "Add";
            form.editLi = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }
    }
}